{% extends 'user_center_base.html' %}

{% load staticfiles %}

{% block title %}天天生鲜-用户中心{% endblock %}

{% block body %}
	<div class="main_con clearfix">
		<div class="left_menu_con clearfix">
			<h3>用户中心</h3>
			<ul>
				<li><a href="{% url 'users:info' %}">· 个人信息</a></li>
				<li><a href="{% url 'orders:info' 1 %}" class="active">· 全部订单</a></li>
				<li><a href="{% url 'users:address' %}">· 收货地址</a></li>
			</ul>
		</div>
		<div class="right_content clearfix">
				<h3 class="common_title2">全部订单</h3>
				{% for order in orders %}
				<ul class="order_list_th w978 clearfix">
					<li class="col01">{{order.create_time}}</li>
					<li class="col02">订单号：{{order.order_id}}</li>
					<li class="col02 stress">{{order.status_name}}</li>
				</ul>

				<table class="order_list_table w980">
					<tbody>
						<tr>
							<td width="55%">
								{% for sku in order.skus %}
								<ul class="order_goods_list clearfix">					
									<li class="col01"><img src="{{sku.default_image.url}}"></li>
									<li class="col02">{{sku.name}}<em>{{sku.price}}/{{sku.unit}}</em></li>
									<li class="col03">{{sku.count}}</li>
									<li class="col04">{{sku.amount}}元</li>
								</ul>
								{% endfor %}
							</td>
							<td width="15%">{{order.total_amount}}元<br/>（含运费{{order.trans_cost}}元）</td>
							<td width="15%">{{order.pay_method_name}}</td>
							<td width="15%"><a href="javascript:;" order_id="{{order.order_id}}" order_status="{{order.status}}" class="oper_btn">
                                {{ order.status_name }}
							</a></td>
						</tr>
					</tbody>
				</table>
				{% endfor %}

				<div class="pagenation">
				{% if orders.has_previous %}
					<a href="{% url 'orders:info' orders.previous_page_number %}">上一页</a>
				{% endif %}
				{% for p in page_list %}
					<a href="{% url 'orders:info' p %}" {% if p == order.number %}class="active"{% endif %}>{{p}}</a>
				{% endfor %}
				{% if orders.has_next %}
					<a href="{% url 'orders:info' orders.next_page_number %}">下一页></a>
				{% endif %}
				</div>
		</div>
	</div>
{% endblock %}

{% block bottom_files %}
	<script type="text/javascript" src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
	<script type="text/javascript">
		$('.oper_btn').click(function() {
			var order_id = $(this).attr("order_id");
			var order_status = $(this).attr("order_status");
			order_status = parseInt(order_status);
			if (1 == order_status) {
			    // 表示订单待支付，向django后端索要支付宝的支付链接
				var req_data = {
				    order_id: order_id,
					csrfmiddlewaretoken: "{{ csrf_token }}"
				};
				$.post('/orders/pay', req_data, function(data){
                    if ( 1 == data.code ) {
                        // 用户未登录
                        location.href = "/users/login";
                    } else if (0 == data.code ) {
                        // 成功拿到了支付连接，开启一个新页面，让用户在新页面进行支付
                        window.open(data.url);

                        $.get("/orders/checkpay?order_id="+order_id, function (resp_data) {
                            if (0 == resp_data.code) {
                                // 支付成功
								alert("支付成功");
                                location.reload();
                            } else {
                                alert(resp_data.message);
                            }
                        });
                    } else {
                        alert(data.message);
                    }
				});
			} else if (4 == order_status) {
			    // window.location.href = ("/orders/comment/" + order_id);
{#				location.href = ("/orders/comment/" + order_id);#}
			} 
		});
	</script>
{% endblock %}
